package com.living.player;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.living.R;
import com.living.player.db.AudioPlayListManger;
import com.living.player.dialog.RecentPlayListDialog;
import com.living.player.event.EventPlayNewData;
import com.living.player.event.EventStatusPause;
import com.living.player.event.EventStatusPlay;
import com.living.player.services.MediaPlayerService;
import com.living.player.services.MediaPlayerServiceImp;
import com.living.player.ui.player.VideoActivity;
import com.living.utils.ImgLoadUtils;
import com.md.basedpc.PHSizeUtils;
import com.md.livingwidget.uiutils.ShapeUtils;
import com.md.network.api.Audio;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

public class BottomPlayerFragment extends Fragment implements View.OnClickListener {


    private View root_player_bg;
    private ImageView iv_bootom_play_status, iv_bootom_play_view;
    private ImageView iv_bottom_audio_cover;
    private TextView tv_bottom_play_title;
    CardView cd_bootom_play_view;
    private RecentPlayListDialog recentPlayListDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        recentPlayListDialog = new RecentPlayListDialog(getActivity());

        recentPlayListDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                refreshState();
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bottom_palyer, container, false);
        root_player_bg = view.findViewById(R.id.root_player_bg);
        iv_bootom_play_status = view.findViewById(R.id.iv_bootom_play_status);
        iv_bootom_play_view = view.findViewById(R.id.iv_bootom_play_view);
        tv_bottom_play_title = view.findViewById(R.id.tv_bottom_play_title);
        cd_bootom_play_view = view.findViewById(R.id.cd_bootom_play_view);
        iv_bottom_audio_cover = view.findViewById(R.id.iv_bottom_audio_cover);

        iv_bootom_play_status.setOnClickListener(this);
        iv_bootom_play_view.setOnClickListener(this);
        tv_bottom_play_title.setOnClickListener(this);
        root_player_bg.setOnClickListener(this);
        root_player_bg.setBackground(ShapeUtils.getDrawableList(0, PHSizeUtils.mm2px(getContext(), 12), 0, Color.parseColor("#8AB6B1")));
        iv_bootom_play_status.setImageResource(MediaPlayerServiceImp.isPlaying() ? R.drawable.icon_player_bottom_pause : R.drawable.icon_player_bottom_play);

        initData();
        return view;
    }

    /**
     * 展示当前播放音频
     */
    private void initData() {
        refreshState();
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshState();
    }

    private void refreshState() {
        List<Audio> audioList = AudioPlayListManger.getInstance().getAllRecentAudios();
        if (MediaPlayerServiceImp.getCurrentPlayAudio() != null && audioList.size() > 0) {
            cd_bootom_play_view.setVisibility(View.VISIBLE);
            Audio audio = MediaPlayerServiceImp.getCurrentPlayAudio();
            ImgLoadUtils.disPlayRound(getActivity(), iv_bottom_audio_cover, audio.getCoverUrl(), R.drawable.icon_player_audio_pic);
            root_player_bg.setBackground(ShapeUtils.getDrawableList(0, PHSizeUtils.mm2px(getContext(), 12), 0, Color.parseColor("#8AB6B1")));
            tv_bottom_play_title.setText(audio.getName() + " - " + audio.getAuthor());
        } else {
            cd_bootom_play_view.setVisibility(View.VISIBLE);
            root_player_bg.setBackground(ShapeUtils.getDrawableList(0, PHSizeUtils.mm2px(getContext(), 12), 0, Color.parseColor("#AAAAAA")));
            ImgLoadUtils.disPlayRound(getActivity(), iv_bottom_audio_cover, "", R.drawable.icon_player_audio_pic);
            tv_bottom_play_title.setText("水流之声");
        }

    }

    @Subscribe
    public void onEvent(Object event) {
        if (event instanceof EventPlayNewData) {
            initData();
            if (recentPlayListDialog != null) {
                recentPlayListDialog.refresh();
            }
        } else if (event instanceof EventStatusPlay) {
            iv_bootom_play_status.setImageResource(R.drawable.icon_player_bottom_pause);
        } else if (event instanceof EventStatusPause) {
            iv_bootom_play_status.setImageResource(R.drawable.icon_player_bottom_play);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_bootom_play_status) {
            if (MediaPlayerServiceImp.getMediaPlayer() == null) {
                if (MediaPlayerServiceImp.getCurrentPlayAudio() != null) {
                    MediaPlayerServiceImp.playAudio(getActivity(), MediaPlayerServiceImp.getCurrentPlayAudio());
                }
            } else {
                if (MediaPlayerService.isPlaying()) {
                    MediaPlayerService.pausePlay();
                } else if (MediaPlayerService.isPlayable()) {
                    MediaPlayerService.startPlay();
                }
            }
        } else if (v.getId() == R.id.iv_bootom_play_view) {
            // 最近播放菜单
            recentPlayListDialog.showDialog();
        } else if (v.getId() == R.id.tv_bottom_play_title || v.getId() == R.id.root_player_bg) {
            startActivity(new Intent(getActivity(), VideoActivity.class));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


}
